﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//
using PerceptiveMCAPI;
using PerceptiveMCAPI.Methods;
using PerceptiveMCAPI.Types;
//
namespace PerceptiveMCAPI_Test
{
   public partial class show_errors : System.Web.UI.UserControl
   {
      public List<Api_Error> apiErrors { get; set; }
      public List<Api_ValidatorMessage> valMessages { get; set; }

      public void Display( List<Api_Error> apiErrors, List<Api_ValidatorMessage> valMessages )
      {
         this.apiErrors = apiErrors;
         this.valMessages = valMessages;
         Display();
      }
      //
      public void Display( List<Api_Error> apiErrors )
      {
         this.apiErrors = apiErrors;
         this.valMessages = new List<Api_ValidatorMessage>();
         Display();
      }
      //
      public void Display( List<Api_ValidatorMessage> valMessages )
      {
         this.valMessages = valMessages;
         this.apiErrors = new List<Api_Error>();
         Display();
      }
      // -----------------------
      public void Display()
      {
         // api 
         if ( apiErrors.Count == 0 )
         {
            divApiError.Visible = false;
            gvApiErrors.DataSource = "";
            gvApiErrors.DataBind();
         }
         else
         {
            this.Visible = true;
            showApiMessages( apiErrors );
         }
         // validator
         if ( valMessages.Count == 0 )
         {
            divValMessage.Visible = false;
            gvValMessage.DataSource = "";
            gvValMessage.DataBind();
         }
         else
         {
            this.Visible = true;
            showValidatorMessages( valMessages );
         }
      }

      // ---------------------------------------------------------------------
      private void showApiMessages( List<Api_Error> msgList )
      {
         divApiError.Visible = true;
         gvApiErrors.DataSource = msgList;
         gvApiErrors.DataBind();
         //
         Panel panel = Parent.FindControl( "pnlResponse" ) as Panel;
         if ( panel != null ) panel.Visible = false;
      }
      private void showValidatorMessages( List<Api_ValidatorMessage> msgList )
      {
         divValMessage.Visible = true;
         gvValMessage.DataSource = msgList;
         gvValMessage.DataBind();
         //
         if ( ApiHelper.ValidatorErrorsExist( msgList ) )
         {
            Panel panel = Parent.FindControl( "pnlResponse" ) as Panel;
            if ( panel != null ) panel.Visible = false;
         }
      }

   }
}